package com.myspring.resttest.dao.impl.continent;

import java.util.List;

import javax.transaction.Transactional;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.myspring.resttest.dao.continent.CountryDaoInt;
import com.myspring.resttest.model.continent.Country;

@EnableTransactionManagement
@Service
@Transactional
public class CountryDaoImpl implements CountryDaoInt {

	@Autowired
	SessionFactory	sessionFactory;

	@Override
	public void insertCountry(Country country) {
		sessionFactory.getCurrentSession().save(country);
	}

	@Override
	public Country getCountryById(int countryId) {
		return (Country) sessionFactory.getCurrentSession().get(Country.class,
				countryId);
	}

	@Override
	public Country getCountry(String countryName) {
		Query query = sessionFactory.getCurrentSession().createQuery(
				"from Country where name = :countryname");
		query.setParameter("coutryname", countryName);
		return (Country) query.list().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Country> getCountries() {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
				Country.class);
		return criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Country> getCountries(Integer id) {
		Query query = sessionFactory.getCurrentSession().createQuery(
				"from Country  where continentd.id = :id");
		query.setParameter("id", id);
		return (List<Country>)query.list();
	}

}
